<template>
  <div>
      <h1>添加课程</h1>
      <!-- 上传文件控件 -->
      <van-cell-group>
          <van-uploader :after-read="afterRead">           
          </van-uploader>
      </van-cell-group>
      <!-- 视频控件 -->
      <van-cell-group>
          <van-field v-model="title" label="课程名:" placeholder="请输入课程名"></van-field>
          <van-field v-model="desc" label="详情:" placeholder="请输入详情"></van-field>
          <van-field v-model="price" label="价格:" placeholder="请输入价格"></van-field>
          <video width="200" height="120" :src="src" controls="controals" autoplay="autoplay"></video>
          <p><van-button @click="addcourse" type="primary">添加课程</van-button></p>
      </van-cell-group>

  </div>
</template>

<script>
import mycate from './mycate.vue'
export default {
    components:{mycate},
    data(){
        return{
            data:{},
            cid:this.$route.query.cid,
            src:'',
            title:'',
            desc:'',
            price:'',
            video:'',
        }
    },
    mounted:function(){},
    methods:{
        get_data:function(){
            this.axios.get('http://127.0.0.1:8000/get_cate').then(resp=>{
                console.log(resp.data);
                var mytree = {'id':0,name:''}
                mytree['children'] = res.data;
                this.data = mytree;
            })
        },
        afterRead:function(file){
            // 定义参数
            let data = new FormData();
            data.append('file',file.file);
            // 创建实例
            const axiosInstance = this.axios.create({withCredentials:false});
            axiosInstance({method:'POST',url:'http://127.0.0.1:8000/upload/',data:data}).then(resp=>{
                console.log(resp);
                this.src = 'http://127.0.0.1:8000/static/upload/' + resp.data.filename
                this.video = resp.data.filename
            })
        },
        addcourse(){
            let data = new FormData();
            data.append('title',this.title);
            data.append('desc',this.desc);
            data.append('price',this.price);
            data.append('cid',this.cid);
            data.append('video',this.video)
            this.axios.post('http://127.0.0.1:8000/add_course/',data).then(resp=>{
                console.log(resp.data)
            }).catch(error=>{
                console.log(error)
            })
        }
    }
}
</script>

<style>

</style>